package com.le.tester.book.concurrentchallenge.threadpool8;

import java.util.concurrent.CountDownLatch;

/**
 * createTime：2025-04-16 10:45
 * description：
 */
public class ValueLatch<T> {

    private T value = null;
    private final CountDownLatch done = new CountDownLatch(1);

    public boolean isSet() {
        return (done.getCount()) == 0;
    }

    public synchronized void setValue(T newValue) {
        if (!isSet()) {
            value = newValue;
            done.countDown();
        }
    }

    public T getValue() throws InterruptedException {
        done.await();
        synchronized (this) {
            return value;
        }
    }

}
